package com.sxhalo.fsms.controller.api;

import com.sxhalo.fsms.controller.BaseController;
import com.sxhalo.fsms.domain.FactDrawingInfo;
import com.sxhalo.fsms.service.IFactDrawingInfoService;
import com.sxhalo.fsms.util.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.UUID;

@Controller
@RequestMapping("/api")
public class DrawingInfoController extends BaseController {

	@Autowired
	private IFactDrawingInfoService roleService;


	@ResponseBody
	@RequestMapping(value = "/drawing", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public ApiResult findDrawing(Integer current, Integer pageSize, HttpServletRequest req) {
		String where = "delete_flag=0";
		String order = "create_time desc";
		if (!isBlank(req.getParameter("drawingName"))) {
			where += " and drawing_name like '%" + req.getParameter("drawingName").trim() + "%'";
		}
		if (!isBlank(req.getParameter("typeId"))) {
			where += " and type_id like '%" + req.getParameter("typeId").trim() + "%'";
		}
		return findList(current, pageSize, where, order, roleService);
	}

	@ResponseBody
	@RequestMapping(value = "/drawing", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ApiResult saveDrawing(@RequestBody  FactDrawingInfo record,  HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setCreateTime(nowtime);
		record.setDeleteFlag(0);
		record.setUpdateTime(nowtime);
        if(record.getDrawingId()!=null&&!record.getDrawingId().equals("")){
			roleService.updateByPrimaryKeySelective(record);
		}else{
			String drawingId = UUID.randomUUID().toString();
			record.setDrawingId(drawingId);
			roleService.insertSelective(record);
		}
		return ApiResult.retOK(req);
	}



	@ResponseBody
	@RequestMapping(value = "/drawing/batchupdate", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ApiResult batchUpdateDrawing( FactDrawingInfo record,  String ids, HttpServletRequest req) {


		roleService.batchUpdate(record,ids);
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@RequestMapping(value = "/drawing", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
	public ApiResult updateDrawing(@RequestBody  FactDrawingInfo record,  HttpServletRequest req) {
		roleService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}


	@ResponseBody
	@RequestMapping(value = "/drawing", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
	public ApiResult deleteDrawing(String  drawingId,  HttpServletRequest req) {
		roleService.deleteByPrimaryKey(drawingId);
		return ApiResult.retOK(req);
	}

	//图纸审核
	@ResponseBody
	@RequestMapping(value = "/drawing/examine", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
	public ApiResult updateDrawinExamine(@RequestBody  FactDrawingInfo record,  HttpServletRequest req) {
		roleService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}

}
